package net.ewon.avatar.integration.agent;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.ewon.avatar.agent.AvatarPackageRepo;
import net.ewon.avatar.agent.AvatarPackageRepoFactory;
import net.ewon.avatar.common.pkg.AvatarPackage;
import net.ewon.avatar.common.pkg.AvatarPackageFile;
import net.ewon.avatar.common.pkg.Version;
import net.ewon.avatar.pkgmgmt.manager.AvatarPackageManager;

import org.apache.commons.io.FileUtils;

public class TestUtils {

	public List<AvatarPackageFile> avatarPackageFiles = new ArrayList<AvatarPackageFile>();

	public AvatarPackage avatarPackage;

	@Resource(name = "avatarPackageManager")
	private AvatarPackageManager avatarPackageManager;

	@Resource
	private AvatarPackageRepoFactory avatarPackageRepoFactory;

	private AvatarPackageRepo avatarPackageRepo;

	public TestUtils() {
		String pkgName = "AvatarAgent";
		Version version = Version.parse("1.0.0");

		String fileName1 = pkgName + File.separator + version + File.separator
				+ "net/ewon/avatar/agent/test1.xml";
		String fileContent1 = "<xml>xmlfiletest1</xml>";
		String fileName2 = pkgName + File.separator + version + File.separator
				+ "test2.xml";
		String fileContent2 = "<xml>xmlfiletest2</xml>";
		avatarPackageFiles.add(new AvatarPackageFile(fileName1, fileContent1
				.getBytes()));
		avatarPackageFiles.add(new AvatarPackageFile(fileName2, fileContent2
				.getBytes()));

		List<String> files = new ArrayList<String>();
		files.add(fileName1);
		files.add(fileName2);

		avatarPackage = new AvatarPackage(pkgName, version, files, "guangzh",
				new Date(System.currentTimeMillis()));
	}

	public void init() {
		if (this.avatarPackageRepoFactory == null) {
			throw new IllegalStateException(
					"The [avatarPackageRepoFactory] property must be set.");
		}
		avatarPackageRepo = avatarPackageRepoFactory.getAvatarPackageRepo();
	}

	public void storePackageToDB() {

		avatarPackageManager.store(avatarPackage);
		for (AvatarPackageFile file : avatarPackageFiles) {
			avatarPackageManager.storeAvatarPackageFile(file);
		}
	}

	public void removePackageFromDB() {
		avatarPackageManager.removePackage(avatarPackage.getId());
		for (AvatarPackageFile file : avatarPackageFiles) {
			avatarPackageManager.removePackageFile(file.getName());
		}
	}

	public AvatarPackage getAvatarPackageFromDB() {
		return avatarPackageManager.getAvatarPackage(avatarPackage.getName(),
				avatarPackage.getVersion());
	}

	public List<AvatarPackageFile> getAvatarPackageFilesFromDB()
			throws IOException {
		List<String> fileNames = avatarPackage.getFileIds();
		return avatarPackageManager.getAvatarPackageFiles(fileNames);
	}

	public void createPackageRepo() {
		avatarPackageRepo.install(avatarPackageFiles);
	}

	public void removePackageRepo() {
		FileUtils.deleteQuietly(new File(avatarPackageRepo
				.getNormalizedBaseLocation()));
	}

}
